import wepy from 'wepy'

export default class simpleList extends wepy.mixin {
  data = {
    options: {page: 1, has_more: true},
    list: []
  }

  methods = {
    
  }

  async onPullDownRefresh () {
    console.log('page is refreshing');
    this.options.page = 1;
    this.options.has_more = true;
    let result = await this.fetchPageData();
    await this.renderPage(result);
    wepy.stopPullDownRefresh();
  }

  async onReachBottom() {
    console.log("onReachBottom");
    if (this.options.has_more) {
      let result = await this.fetchPageData();
      this.renderPage(result);
    }
  }

  async renderPage(result) {
    console.log(result);
    if (result.ret == 0) {
      //新结构，使用 API Resource
      if (result.data.meta) {
        if (result.data.data.length == 0) {
          this.options.has_more = false;
        } else {
          this.options.page++;

          if (result.data.meta.current_page == 1) {
            this.list = result.data.data;
          } else {
            this.list = [...this.list, ...result.data.data];
          }
        }
      } else {
          if (result.data.list.length == 0) {
          this.options.has_more = false;
        } else {
          this.options.page++;
          if (result.data.pagination.current_page == 1) {
            this.list = result.data.list;
          } else {
            this.list = [...this.list, ...result.data.list];
          }
        }
      }
      
      this.$apply();
    } else {
      wepy.showToast({
        title: result.msg,
        icon: "none",
        duration: 3000,
        mask: true
      });
    }
  }
  
}
